//
//  HTCommentBoardView.m
//  TruckDriver
//
//  Created by Ting Han on 2017/10/25.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "HTCommentBoardView.h"
#import "HTCommentBoardCell.h"
#import "UIView+Layout.h"

@interface HTCommentBoardView()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UIView *tapNormal;
@property (weak, nonatomic) IBOutlet UIView *tapBad;
@property (weak, nonatomic) IBOutlet UIView *tapGood;
@property (weak, nonatomic) IBOutlet UIImageView *normalImageView;
@property (weak, nonatomic) IBOutlet UIImageView *badImageView;
@property (weak, nonatomic) IBOutlet UIImageView *goodImageView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flowLayout;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *collectionHeight;
@property (weak, nonatomic) IBOutlet UILabel *hiddenLabel;


@property (nonatomic,strong) NSArray *dataSource;
@property (nonatomic,strong) NSArray *goodData;
@property (nonatomic,strong) NSArray *normalData;
@property (nonatomic,strong) NSArray *badData;
@property (weak, nonatomic) IBOutlet UIView *hiddenView;


@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textHeight;
@property (weak, nonatomic) IBOutlet UILabel *bottomLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttonHeight;

@end

@implementation HTCommentBoardView
- (void)awakeFromNib{
    [super awakeFromNib];
    self.commitButtom.backgroundColor = kBlueColor;
    self.goodData = @[@"运费合理",@"货运真实",@"回款及时",@"态度好"];
    self.normalData = @[@"货源信息不符",@"备货时间长",@"装卸地址不符"];
    self.badData = @[@"态度恶劣",@"回款慢",@"货源信息严重不符"];
    self.selectedData = [NSMutableArray array];
    [self.flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    self.flowLayout.itemSize = CGSizeMake((self.frame.size.width - kWidth(98))/2, kWidth(30));
    self.flowLayout.minimumLineSpacing = kWidth(8);
    self.flowLayout.minimumInteritemSpacing = 0;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(kWidth(20), kWidth(20), kWidth(20), kWidth(20));
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"HTCommentBoardCell" bundle:nil] forCellWithReuseIdentifier:CommentBoardCell];
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapGesture:)];
     UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapGesture:)];
     UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapGesture:)];
    self.tapGood.tag = 151;
    self.tapNormal.tag = 152;
    self.tapBad.tag = 153;
    [self.tapGood addGestureRecognizer:tap1];
    [self.tapNormal addGestureRecognizer:tap2];
    [self.tapBad addGestureRecognizer:tap3];
    self.selectedIndex = 0;
    self.dataSource = [NSArray array];
    self.hiddenView.hidden = YES;
    self.collectionHeight.constant = 0;
    self.buttonHeight.constant = 0;
    
}
- (void)reloadMyCollectionView{
    self.hiddenView.hidden = NO;
    self.textHeight.constant = 0;
    self.buttonHeight.constant = 40;
    if (self.dataSource.count%2 == 0) {
        self.collectionHeight.constant = kWidth(48) + self.dataSource.count/2 * kWidth(30);
    }else{
         self.collectionHeight.constant = kWidth(48) + (self.dataSource.count/2 + 1) * kWidth(30);
    }
    [self layoutIfNeeded];
    [self.collectionView reloadData];
    if (self.delegate &&[self.delegate respondsToSelector:@selector(didChangeHeight:)]) {
        [self.delegate didChangeHeight:self.bottomLabel.bottom];
    }
}
- (void)didTapGesture:(UITapGestureRecognizer *)sender{
    switch (sender.view.tag) {
        case 151://1
        {
            if (self.selectedIndex == 3) {
                return;
            }
            self.selectedIndex = 3;
            [self tapViewIndex:1];
            self.dataSource = self.goodData;
            [self reloadMyCollectionView];
            [self.selectedData removeAllObjects];
        }
            break;
        case 152://2
        {
            if (self.selectedIndex == 2) {
                return;
            }
            self.selectedIndex  = 2;
            [self tapViewIndex:2];
            self.dataSource = self.normalData;
            [self reloadMyCollectionView];
            [self.selectedData removeAllObjects];
        }
            break;
        case 153://1
        {
            if (self.selectedIndex == 1) {
                return;
            }
            self.selectedIndex = 1;
            [self tapViewIndex:3];
            self.dataSource = self.badData;
            [self reloadMyCollectionView];
            [self.selectedData removeAllObjects];
        }
            break;
            
        default:
            break;
    }
}
- (void)tapViewIndex:(NSInteger)index{
    
    if (index == 1) {
        self.goodImageView.image = [UIImage imageNamed:@"icon-highpraise-select"];
        self.badImageView.image = [UIImage imageNamed:@"icon-lowpraise-noselect"];
        self.normalImageView.image = [UIImage imageNamed:@"icon-middlepraise-noselect"];
        
    }else if(index == 2){
        self.goodImageView.image = [UIImage imageNamed:@"icon-highpraise-noselect"];
         self.badImageView.image = [UIImage imageNamed:@"icon-lowpraise-noselect"];
         self.normalImageView.image = [UIImage imageNamed:@"icon-middlepraise-select"];
    }else if(index == 3){//3
        self.goodImageView.image = [UIImage imageNamed:@"icon-highpraise-noselect"];
         self.badImageView.image = [UIImage imageNamed:@"icon-lowpraise-select"];
        self.normalImageView.image = [UIImage imageNamed:@"icon-middlepraise-noselect"];
    }
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((self.frame.size.width - kWidth(70))/2, kWidth(30));
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HTCommentBoardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CommentBoardCell forIndexPath:indexPath];
    if (self.selectedIndex == 2 || self.selectedIndex == 3) {
        cell.iconImageView.image = [UIImage imageNamed:@"icon_noselect_handparise"];
    }else{
        cell.iconImageView.image = [UIImage imageNamed:@"icon_handparise_noselect_bottom"];
    }
    cell.isChosed = NO;
    cell.iconLabel.text = self.dataSource[indexPath.row];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HTCommentBoardCell *cell = (HTCommentBoardCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if (!cell.isChosed && self.selectedData.count >= 3) {
        [HTShowUtil showMessage:@"最多选3个"];
        return;
    }
    cell.isChosed = !cell.isChosed;
    if (cell.isChosed) {
        [self.selectedData addObject:self.dataSource[indexPath.row]];
    }else{
        if ([self.selectedData containsObject:self.dataSource[indexPath.row]]) {
            [self.selectedData removeObject:self.dataSource[indexPath.row]];
        }
    }
}


@end
